package com.firexmoon.hoverclock

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    
    companion object {
        private const val REQUEST_CODE_OVERLAY_PERMISSION = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.btnStartFloatingClock).setOnClickListener {
            if (!checkOverlayPermission()) {
                requestOverlayPermission()
            } else {
                startFloatingService()
                hideMainActivity()
            }
        }
        
        findViewById<Button>(R.id.btnCloseFloatingClock).setOnClickListener {
            stopFloatingService()
        }
        
        // 应用启动时自动检查权限并显示悬浮窗
        if (!checkOverlayPermission()) {
            requestOverlayPermission()
        } else {
            startFloatingService()
            hideMainActivity()
        }
    }
    
    override fun onResume() {
        super.onResume()
        
        // 每次主界面显示到前台时，如果悬浮窗未显示且有权限，则显示悬浮窗并隐藏主界面
        if (checkOverlayPermission() && !isServiceRunning(FloatingService::class.java)) {
            startFloatingService()
            hideMainActivity()
        }
    }

    private fun checkOverlayPermission(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Settings.canDrawOverlays(this)
        } else {
            true
        }
    }

    private fun requestOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val intent = Intent(
                Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:$packageName")
            )
            startActivityForResult(intent, REQUEST_CODE_OVERLAY_PERMISSION)
        }
    }

    private fun startFloatingService() {
        val serviceIntent = Intent(this, FloatingService::class.java)
        startService(serviceIntent)
    }
    
    private fun stopFloatingService() {
        val serviceIntent = Intent(this, FloatingService::class.java)
        serviceIntent.action = FloatingService.ACTION_STOP_SERVICE
        startService(serviceIntent)
    }
    
    private fun hideMainActivity() {
        // 隐藏主界面
        moveTaskToBack(true)
    }
    
    private fun isServiceRunning(serviceClass: Class<*>): Boolean {
        val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        
        // 获取当前运行的服务
        val runningServices = activityManager.getRunningServices(Integer.MAX_VALUE)
        
        // 检查我们的服务是否在运行中的服务列表中
        for (serviceInfo in runningServices) {
            if (serviceClass.name == serviceInfo.service.className) {
                return true
            }
        }
        return false
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_OVERLAY_PERMISSION) {
            if (checkOverlayPermission()) {
                startFloatingService()
                hideMainActivity()
            } else {
                Toast.makeText(this, "悬浮窗权限未授予", Toast.LENGTH_SHORT).show()
            }
        }
    }
} 